package com.ym.utils;


import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;

public class PDDShopUtils {

    public static String clientShop_id = "4b7c698de0f9446eaf99b3c5b34c6a83";
    public static String clientShop_secret = "19edb0cbb320b51f95b2b4126f4d7697f66e5b5c";
    private static String access_token_url = "http://open-api.pinduoduo.com/oauth/token";

    public static String return_clientShop_id = "qRJPTyraZaxFn1BP";
    public static String return_clientShop_secret = "93Ig71b2kIsA8SHS92yBItXwFr3t4Psc";
    private static String return_access_token_url = "http://132.232.138.242/oauth/token";


    public static void main(String[] args) {
        Map<String,Object> paramMap = new HashMap<>();
        Map<String,Object> rMap = new HashMap<>();
        paramMap.put("client_id",return_clientShop_id);
        paramMap.put("client_secret",return_clientShop_secret);
        paramMap.put("grant_type ","grant_type");
        paramMap.put("refresh_token","d812f281829c4e46b47bf9594a058eecD5250D605F5BEA242665BDBB7C8F06C2");
        paramMap.put("state","1212");
        String resulet = HttpUtils.postJson(return_access_token_url,paramMap);
//        {"error_response":{"error_code":43002,"error_msg":"Content-Type只支持application/json"}}
        JSONObject job = new JSONObject(resulet);
        String access_token = "";
        String refresh_token = "";
        try {
            access_token = Convert.objToStr(job.get("access_token"));
            refresh_token = Convert.objToStr(job.get("refresh_token"));
            rMap.put("access_token",access_token);
            rMap.put("refresh_token",refresh_token);
        }catch (Exception e){
            System.out.println(resulet);
        }
        System.out.println(rMap.toString());
    }

    public static Map<String,Object> getAccessToken(String code){
        Map<String,Object> paramMap = new HashMap<>();
        Map<String,Object> rMap = new HashMap<>();
        paramMap.put("client_id",clientShop_id);
        paramMap.put("client_secret",clientShop_secret);
        paramMap.put("grant_type","authorization_code");
        paramMap.put("redirect_uri","https://duoke678.com/");
        paramMap.put("code",code);
        paramMap.put("state","1212");
        String resulet = HttpUtils.postJson(access_token_url,paramMap);
//        {"error_response":{"error_code":43002,"error_msg":"Content-Type只支持application/json"}}
        JSONObject job = new JSONObject(resulet);
        String access_token = "";
        String refresh_token = "";
        try {
            access_token = Convert.objToStr(job.get("access_token"));
            refresh_token = Convert.objToStr(job.get("refresh_token"));
            rMap.put("access_token",access_token);
            rMap.put("refresh_token",refresh_token);
        }catch (Exception e){
            System.out.println(resulet);
        }
        return rMap;
    }

    public static Map<String,Object> returnAccessTokenRefresh(String refresh_token){
        Map<String,Object> paramMap = new HashMap<>();
        Map<String,Object> rMap = new HashMap<>();
        paramMap.put("client_id",return_clientShop_id);
        paramMap.put("client_secret",return_clientShop_secret);
        paramMap.put("grant_type","refresh_token");
        paramMap.put("refresh_token",refresh_token);
        paramMap.put("state","1212");
        String resulet = HttpUtils.postJson(return_access_token_url,paramMap);
//        {"error_response":{"error_code":43002,"error_msg":"Content-Type只支持application/json"}}
        JSONObject job = new JSONObject(resulet);
        String access_token = "";
        String r_refresh_token = "";
        try {
            access_token = Convert.objToStr(job.get("access_token"));
            r_refresh_token = Convert.objToStr(job.get("refresh_token"));
            rMap.put("access_token",access_token);
            rMap.put("refresh_token",r_refresh_token);
        }catch (Exception e){
            System.out.println(resulet);
        }
        return rMap;
    }
    public static Map<String,Object> getAccessTokenRefresh(String refresh_token){
        Map<String,Object> paramMap = new HashMap<>();
        Map<String,Object> rMap = new HashMap<>();
        paramMap.put("client_id",clientShop_id);
        paramMap.put("client_secret",clientShop_secret);
        paramMap.put("grant_type","refresh_token");
        paramMap.put("refresh_token",refresh_token);
        paramMap.put("state","1212");
        String resulet = HttpUtils.postJson(access_token_url,paramMap);
//        {"error_response":{"error_code":43002,"error_msg":"Content-Type只支持application/json"}}
        JSONObject job = new JSONObject(resulet);
        String access_token = "";
        String r_refresh_token = "";
        try {
            access_token = Convert.objToStr(job.get("access_token"));
            r_refresh_token = Convert.objToStr(job.get("refresh_token"));
            rMap.put("access_token",access_token);
            rMap.put("refresh_token",r_refresh_token);
        }catch (Exception e){
            System.out.println(resulet);
        }
        return rMap;
    }
    public static List<Map<String,Object>> result2list(JSONArray orderList){
        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String,Object> resultMap = null;
        Iterator<Object> ianswers  = orderList.iterator();
        while (ianswers.hasNext()) {
            resultMap = new HashMap<>();
            String str = ianswers.next().toString();
            JSONObject obj =  new JSONObject(str);
            Iterator<String> nameList = obj.keys();
            while (nameList.hasNext()) {
                String key = nameList.next().toString();
                String value = obj.get(key).toString();
                resultMap.put(key, value);
            }
            resultList.add(resultMap);
        }
        return resultList;
    }


    public static String sign(Map<String,Object> paramMap){
        Map<String,Object> treeMap = new TreeMap<>();
        for (Map.Entry<String, Object> m : paramMap.entrySet()) {
            treeMap.put(m.getKey(),m.getValue());
        }
        String pStr = maptoShopStr(treeMap);
        String sign = PublicUtils.MD5(pStr).toUpperCase();
//        String a = "testSecretaccess_tokenasd78172s8ds9a921j9qqwda12312w1w21211client_id1data_typeXMLorder_status1page1page_size10timestamp1480411125typepdd.order.number.list.gettestSecret";
//        String sign = PublicUtils.MD5(a).toUpperCase();
        System.err.println(pStr);

        return sign;
    }
    //map转换成字符串
    private static String maptoShopStr(Map<String, Object> treeMap) {
        StringBuffer pStr = new StringBuffer();
        pStr.append(return_clientShop_secret);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            pStr.append(m.getKey());
            pStr.append(m.getValue());
        }
        pStr.append(return_clientShop_secret);
        return pStr.toString();
    }

    public static String signRequest(Map<String, Object> resultMap, final String secret) {
        Objects.requireNonNull(resultMap);

        String[] keys = resultMap.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        StringBuilder sb = new StringBuilder(secret);
        for (String key : keys) {
            String asJson = resultMap.get(key).toString();
            sb.append(key).append(asJson);
        }
        sb.append(secret);
        String signStr = sb.toString();
        System.err.println(signStr);
        String calcSignMd5 = DigestUtils.md5Hex(signStr).toUpperCase();

        return calcSignMd5;

    }
}
